package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"goORMTest/goJdbcTest/models"
	"log"
	"net/http"
	"strconv"
)

func IndexApi(c *gin.Context) {
	c.String(http.StatusOK, "It works")
}

func AddPersonApi(c *gin.Context) {
	firstName := c.Request.FormValue("first_name")
	lastName := c.Request.FormValue("last_name")
	p := models.PersonDo{FirstName: firstName, LastName: lastName}
	ra, err := p.AddPerson()
	if err != nil {
		log.Fatalln(err)
	}
	msg := fmt.Sprintf("insert successful %d", ra)
	c.JSON(http.StatusOK, gin.H{"msg": msg})
}
func GetPersonApi(c *gin.Context) {
	id := c.Query("id")
	//string转成int：
	idint, err := strconv.Atoi(id)
	p := models.PersonDo{}
	pe, err := p.GetPerson(idint)
	if err != nil {
		log.Fatalln(err)
	}
	msg := fmt.Sprintf("select successful %d", pe)
	c.JSON(http.StatusOK, gin.H{"msg": msg})
}
